﻿/* 
 * Copyright 2010 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Ngilead.Core.Exception;
using Ngilead.Core.Utils;
using Ngilead.Core.Replicator;
using Ngilead.Common;

namespace Ngilead.Core.Store.Stateless
{
    /// <summary>
    /// Stateless proxy store.
    /// The proxy informations is stored on the pojo, by implementing the ILightEntity
    /// interface.
    /// </summary>
    public class StatelessProxyStore : IProxyStore
    {
        /*----------------------------------------------------------------------------------------*/
        #region Private static variables

        /// <summary>
        /// Logger channel
        /// </summary>
        private static ILog logger = LogManager.GetLogger(typeof(StatelessProxyStore));

        #endregion
        /*----------------------------------------------------------------------------------------*/

        /*----------------------------------------------------------------------------------------*/
        #region Ctor

        /// <summary>
        /// Constructor
        /// </summary>
        public StatelessProxyStore()
        {
        }

        #endregion
        /*----------------------------------------------------------------------------------------*/

        /*----------------------------------------------------------------------------------------*/
        #region IProxyStore implementation
        
        /// <summary>
        /// Store proxy informations
        /// </summary>
        /// <param name="cloneBean">Clone</param>
        /// <param name="persistentBean">Persistent object</param>
        /// <param name="propertyName">Property name</param>
        /// <param name="proxyInformations">Proxy informations</param>
        public void StoreProxyInformations(object cloneBean, object persistentBean,
                                           string propertyName,
                                           Dictionary<string, object> proxyInformations)
        {
            // ILightEntity checking
            ILightEntity temp = cloneBean as ILightEntity;
            if (temp == null)
            {
                if (cloneBean != null)
                {
                    throw new ProxyStoreException("Class " + cloneBean.GetType().ToString() + " must implements ILightEntity interface !", cloneBean);
                }
                else
                {
                    throw new ProxyStoreException("null Class  must implements ILightEntity interface !", cloneBean);
                }
            }

            //	Extract initialization info
            if (proxyInformations != null)
            {
                if (proxyInformations.Keys.Contains(Constants.INITIALISED))
                {
                //  Store initialization map
                //
                    bool initialized = (bool)proxyInformations[Constants.INITIALISED];
                    ((ILightEntity)cloneBean).SetInitialized(propertyName, initialized);

                    // remove from map
                    proxyInformations.Remove(Constants.INITIALISED);
                }

            //  Store proxy informations
            //
                ((ILightEntity)cloneBean).AddProxyInformation(propertyName, proxyInformations);
            }

        }

        /// <summary>
        /// Remove proxy informations
        /// </summary>
        /// <param name="pojo">Object</param>
        /// <param name="propertyName">Property name</param>
        public void RemoveProxyInformations(object pojo, string propertyName)
        {
            //	ILightEntity checking
            if (pojo is ILightEntity)
            {
                //	Remove information from the POJO
                ((ILightEntity)pojo).RemoveProxyInformation(propertyName);
            }
        }

        /// <summary>
        /// Get proxy informations
        /// </summary>
        /// <param name="pojo">Object</param>
        /// <param name="propertyName">Property name</param>
        /// <returns>Proxy informations</returns>
        public Dictionary<string, object> GetProxyInformations(object pojo, string propertyName)
        {
            //	ILightEntity checking
            if (pojo is ILightEntity == false)
            {
                return null;
            }

            Dictionary<string, object> proxyInformations = ((ILightEntity)pojo).GetProxyInformation(propertyName) 
                                                           as Dictionary<string, object>;

            //	Add initialization information
            //
            bool initialized = ((ILightEntity)pojo).IsInitialized(propertyName);
            if (initialized == false)
            {
                proxyInformations[Constants.INITIALISED] = false;
            }

            return proxyInformations;
        }

        /// <summary>
        /// CleanUp
        /// </summary>
        public void CleanUp()
        {
        }

        #endregion
        /*----------------------------------------------------------------------------------------*/

        /*----------------------------------------------------------------------------------------*/
        #region Internal methods

        #endregion
        /*----------------------------------------------------------------------------------------*/
    }
}
